package cn.com.fuhuitong.main.access.ui.fragment

import android.os.Bundle
import cn.com.fuhuitong.base.BaseAbstractFragment
import com.jakewharton.rxbinding2.widget.RxTextView
import cn.com.fuhuitong.R
import cn.com.fuhuitong.main.order.ui.fragment.OrderListFragment
import io.reactivex.Observable
import io.reactivex.disposables.CompositeDisposable
import io.reactivex.disposables.Disposable
import io.reactivex.functions.Function4
import kotlinx.android.synthetic.main.fragment_access_change.*

class AccessChangeFragment : BaseAbstractFragment() {

    private val dispose: CompositeDisposable = CompositeDisposable()

    companion object {
        const val TITLE = "title"
        fun newInstance(title: String = "忘记密码"): AccessChangeFragment {
            val fragment = AccessChangeFragment()
            fragment.arguments = Bundle().apply {
                putString(TITLE, title)
            }
            return fragment
        }
    }

    override val layoutResId: Int
        get() = R.layout.fragment_access_change

    override fun initView() {

        text_change_title.text = arguments?.getString(TITLE)

        val observablePhone: Observable<CharSequence> =
            RxTextView.textChanges(edit_change_phone)
                .doOnNext {
                    text_change_code.isEnabled = it.isNotEmpty()
                }
        val observableCode: Observable<CharSequence> =
            RxTextView.textChanges(edit_change_code)
        val observablePassword: Observable<CharSequence> =
            RxTextView.textChanges(edit_change_password)
        val observablePasswordSure: Observable<CharSequence> =
            RxTextView.textChanges(edit_change_password_sure)

        val observable: Disposable = Observable.combineLatest(
            observablePhone, observableCode, observablePassword, observablePasswordSure,
            Function4<CharSequence?, CharSequence, CharSequence, CharSequence, Boolean> { t1, t2, t3, t4 ->
                t1.isNotEmpty() && t2.isNotEmpty() && t3.isNotEmpty() && t4.isNotEmpty()
            })
            .subscribe {
                text_change_submit.isEnabled = it
            }
        dispose.add(observable)
//        Observable.interval(0, 1, TimeUnit.SECONDS)
//            .take(10)
//            .observeOn(AndroidSchedulers.mainThread())
//            .subscribe(object : Observer<Long> {
//                override fun onComplete() {
//                    LogUtils.e("结束")
//                }
//
//                override fun onSubscribe(d: Disposable) {
//                    LogUtils.e("开始")
//                }
//
//                override fun onNext(t: Long) {
//
//                    LogUtils.e("$t")
//                }
//
//                override fun onError(e: Throwable) {
//
//                }
//            })

    }

    override fun onDestroyView() {
        super.onDestroyView()
        if (dispose.size() > 0 && !dispose.isDisposed) {
            dispose.dispose()
        }
    }
}